# 首先 import 必要的模块
import pandas as pd 
import numpy as np

from sklearn.model_selection import GridSearchCV

import matplotlib.pyplot as plt

from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import LogisticRegression
#%matplotlib inline


# 读取数据

train = pd.read_csv('FE_pima-indians-diabetes.csv')

y_train = train['Target']   
X_train = train.drop(["Target"], axis=1)

#保存特征名字以备后用（可视化）
feat_names = X_train.columns 

#需要调优的参数
# 请尝试将L1正则和L2正则分开，并配合合适的优化求解算法（slover）
penaltys = ['l1','l2']

#训练数据多，C可以大一点（更多相信数据）
Cs = [0.01, 0.1, 1, 10, 100, 1000, 10000]

tuned_parameters = dict(penalty = penaltys, C = Cs)#组合调优参数

lr_penalty= LogisticRegression()
#grid= GridSearchCV(lr_penalty, tuned_parameters,cv=5, scoring='neg_log_loss')#log似然损失

grid= GridSearchCV(lr_penalty, tuned_parameters,cv=5, scoring='accuracy')#正确率

grid.fit(X_train,y_train)

# examine the best model
print(-grid.best_score_)#打印模型参数
print(grid.best_params_)


#绘制CV误差曲线分析模型
# plot CV误差曲线
test_means = grid.cv_results_[ 'mean_test_score' ]
test_stds = grid.cv_results_[ 'std_test_score' ]
train_means = grid.cv_results_[ 'mean_train_score' ]
train_stds = grid.cv_results_[ 'std_train_score' ]


# plot results
n_Cs = len(Cs)
number_penaltys = len(penaltys)
test_scores = np.array(test_means).reshape(n_Cs,number_penaltys)
train_scores = np.array(train_means).reshape(n_Cs,number_penaltys)
test_stds = np.array(test_stds).reshape(n_Cs,number_penaltys)
train_stds = np.array(train_stds).reshape(n_Cs,number_penaltys)

x_axis = np.log10(Cs)
for i, value in enumerate(penaltys):
    #pyplot.plot(log(Cs), test_scores[i], label= 'penalty:'   + str(value))
    plt.errorbar(x_axis, -test_scores[:,i], yerr=test_stds[:,i] ,label = penaltys[i] +' Test')
    #plt.errorbar(x_axis, -train_scores[:,i], yerr=train_stds[:,i] ,label = penaltys[i] +' Train')
    
plt.legend()
plt.xlabel( 'log(C)' )                                                                                                      
plt.ylabel( 'logloss' )
plt.savefig('LogisticGridSearchCV_C.png' )

plt.show()

import cPickle

cPickle.dump(grid.best_estimator_, open("FE_pima-indians-diabetes", 'wb'))
